import {random_move,get_valid_moves} from "/go/random_move.js"
import {get_neighbors_4,get_neighbors_8} from "/go/get_neighbors.js"
import {score_of_step,score_of_step_list} from "/go/score_of_step.js"
import {first_step} from "/go/first_step.js"
import {is_game_begin} from "/go/is_game_begin.js"
import {defend_drop} from "/go/defend-drop.js"
import {figure_out_eat_drop} from "/go/eat.js"
import {is_impossible_to_get_a_point} from "/go/impossible-to-get-a-point.js"
                                                


const c_length = 5;// N X N 棋盘大小 最大的是13X13
const c_opponent_name = 'The Black Hand';//'Netburners'前期升级hacknet//'The Black Hand';//'Illuminati'(加hack速度) // 'Daedalus' 加声望

/**
 * 自动落子
 * @return true:落子成功,  false落子失败
 */
async function drop(ns)
{

  var chains = ns.go.analysis.getChains();
  var board_state = ns.go.getBoardState();
  const valid_moves = ns.go.analysis.getValidMoves();
  var liberties = ns.go.analysis.getLiberties();
  

  // // ----    以上是 取分析数据    ----

  if(await first_step(board_state,ns))
  {
    board_state = await ns.go.getBoardState();
    ns.print("走了第一步");
    return;
  }
  
  // 这局不可能取到眼
  if(is_impossible_to_get_a_point(ns,board_state))
  {
    await ns.go.resetBoardState(c_opponent_name,c_length);
    return;
  }

  // 防御 跑 
  var defend_moves = defend_drop(ns,chains,board_state,valid_moves);
  if(defend_moves.length)
  {
    let index = Math.floor(Math.random() * defend_moves.length);
    ns.print("防御落子:",defend_moves[index]);
    //落子:
    var move_result= await ns.go.makeMove(defend_moves[index][0],defend_moves[index][1]);
    return;
  }

  // 吃, 有得吃就吃, 不然吃亏
  let pos_for_eat = figure_out_eat_drop(ns,board_state,chains,valid_moves);
  if(pos_for_eat.length)
  {
    ns.print("吃对方的链:",pos_for_eat);
    await ns.go.makeMove(pos_for_eat[0],pos_for_eat[1]);
    return;
  }

  

  // ns.print("随机的移动:",random_move(board_state,valid_moves,ns));
  var valid_moves_list = get_valid_moves(valid_moves);
  // ns.print("取所有的可移动点:",valid_moves_list);
  


  var score_list = score_of_step_list(board_state,valid_moves_list,chains,ns);
  // ns.print("取到分数表:" );
  // for(var i=0;i<score_list.length;++i)
  // {
  //   ns.print("\t",score_list[i].x,",",score_list[i].y,"\t",score_list[i].score);
  // }

  // 在分数表中随机一步:
  if(score_list.length)
  {
    const max_score = score_list[0].score;
    var size_of_max_score = 0;
    for(var i=0;i<score_list.length;++i)
    {
      // 防范分数过低的情况, 过低就不走了, 不然自己被堵死
      if(max_score == score_list[i].score && score_list[i].score>-3)
      {
        size_of_max_score ++;
      }
      else
      {
        break;
      }
    }
    if(0==size_of_max_score)
    {
      ns.print("!!!!    没步了 199");
      //await ns.go.resetBoardState(c_opponent_name,c_length);
      //ns.tprint("ns.go.passTurn(); 219");
      await ns.go.opponentNextTurn();
      await ns.go.passTurn();
    }
    else
    {
      var indexRandom = Math.floor(Math.random() * size_of_max_score);
      //ns.print("随机:",indexRandom,"\t高分有几个:",size_of_max_score);
      ns.print("随机到的步:",score_list[indexRandom].x,",",score_list[indexRandom].y);
      //落子:
      var move_result= await ns.go.makeMove(score_list[indexRandom].x,score_list[indexRandom].y);
    }
    
    
  }
  else
  {
    ns.print("!!!!    没步了");
    //await ns.go.resetBoardState(c_opponent_name,c_length);
    // ns.tprint("ns.go.passTurn(); 221");
    await ns.go.opponentNextTurn();
    await ns.go.passTurn();
  }
}

/*                                                                                                                                                             */
/* *********************************************************************************************************************************************************** */
/*                                                                                                                                                             */
/* *********************************************************************************************************************************************************** */
/*                                                                                                                                                             */
/* *********************************************************************************************************************************************************** */
/*                                                                                                                                                             */
/* *********************************************************************************************************************************************************** */
/*                                                                                                                                                             */
export async function main(ns) {
  while(true)
  {
    ns.print("============================");
    var bool_game_begin = is_game_begin(ns);
    var game_state = ns.go.getGameState();
    var bool_game_over = false;
    // ns.print("游戏状态：", game_state);
    if(null==game_state.previousMove)
    {
      ns.print("\t\t游戏为结束状态");
      bool_game_over = true;
    }
    if("None"==game_state["currentPlayer"])
    {
      ns.print("\t\t游戏为结束状态,重动新一局");
      await ns.go.resetBoardState(c_opponent_name,c_length);
      bool_game_over = true;
    }
    if(bool_game_over && !bool_game_begin)
    {
      //await ns.sleep(100);
      //break;

      if("Black"==game_state["currentPlayer"]) 
      {
        // 这里走到不能再走, 会自己执行passTurn;
        await drop(ns);
        // ns.print("ns.go.passTurn();");
        // await ns.go.passTurn();
      }
      else
      {
        // ns.tprint("currentPlayer:",game_state["currentPlayer"]);
        //await ns.sleep(1000);
        await ns.sleep(1);// 没这句的话, 重启会卡死.
        continue;
      }
    }
    else
    {
      // ns.print("212 go drop");
      await drop(ns);
      //await ns.sleep(1);
    }
  }// END while(true);
  
  ns.print("程序结束");
}